package com.boot.ys.mybatisplus.script;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashSet;
import java.util.Set;

/**
 * 描述 微信小程序前后台文件夹代码合并为一个文件夹，前缀相同就行
 *
 * @Author yangsheng
 * @Date 2024/8/16 23:32
 */
public class WxImageTwo {


    public static void main(String[] args) throws IOException {

        HashMap<Object, Object> allMap = new HashMap<>();
        // 输入
        Path sourceDir = Paths.get("E:\\BaiduNetdiskDownload\\1\\微信小程序2.0合并\\");

        // 输出文件夹
        Path targetDir = Paths.get("E:\\BaiduNetdiskDownload\\1\\微信小程序项目2.0截图");

        // 获取目标目录中的所有子文件夹前9位名称
        Set<String> targetFolderNames = getTargetFolderNames(targetDir, allMap);

        try {
            Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    // 只处理图片文件
                    String fileName = file.getFileName().toString();
                    if (fileName.toLowerCase().endsWith(".jpg") ||
                            fileName.toLowerCase().endsWith(".jpeg") ||
                            fileName.toLowerCase().endsWith(".png") ||
                            fileName.toLowerCase().endsWith(".bmp") ||
                            fileName.toLowerCase().endsWith(".gif")) {

                        // 获取当前文件所在目录的名称的前9个字符
                        Path parentDir = file.getParent();
                        String parentFolderName = parentDir.getFileName().toString();
                        String folderPrefix = parentFolderName.length() >= 9 ? parentFolderName.substring(0, 9) : parentFolderName;

                        // 如果目标目录中存在对应的前9位子文件夹，则复制文件
                        if (targetFolderNames.contains(folderPrefix)) {

                            Path targetSubDir = targetDir.resolve(allMap.get(folderPrefix).toString());

                            // 构建目标文件路径
                            Path targetFile = targetSubDir.resolve(file.getFileName());
                            Files.copy(file, targetFile, StandardCopyOption.REPLACE_EXISTING);
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
            System.out.println("文件复制完成。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取目标目录中所有子文件夹前9位名称
    public static Set<String> getTargetFolderNames(Path targetDir, HashMap map) throws IOException {
        Set<String> folderNames = new HashSet<>();
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(targetDir)) {
            for (Path entry : stream) {
                if (Files.isDirectory(entry)) {
                    String folderName = entry.getFileName().toString();
                    String prefix = folderName.length() >= 9 ? folderName.substring(0, 9) : folderName;
                    folderNames.add(prefix);

                    map.put(prefix, folderName);
                }
            }
        }
        return folderNames;
    }

}
